<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>image_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_net_image_test.js.html">image_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for goog.labs.net.Image.
<a name="line17"></a> *
<a name="line18"></a> * @author nnaze@google.com (Nathan Naze)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>/** @suppress {extraRequire} */
<a name="line23"></a>goog.provide(&#39;goog.labs.net.imageTest&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.events&#39;);
<a name="line26"></a>goog.require(&#39;goog.labs.net.image&#39;);
<a name="line27"></a>goog.require(&#39;goog.result&#39;);
<a name="line28"></a>goog.require(&#39;goog.result.Result&#39;);
<a name="line29"></a>goog.require(&#39;goog.string&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line31"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line32"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line33"></a>
<a name="line34"></a>goog.setTestOnly(&#39;goog.labs.net.ImageTest&#39;);
<a name="line35"></a>
<a name="line36"></a>var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
<a name="line37"></a>
<a name="line38"></a>function testValidImage() {
<a name="line39"></a>  var url = &#39;testdata/cleardot.gif&#39;;
<a name="line40"></a>
<a name="line41"></a>  asyncTestCase.waitForAsync(&#39;image load&#39;);
<a name="line42"></a>
<a name="line43"></a>  assertEquals(0, goog.events.getTotalListenerCount());
<a name="line44"></a>
<a name="line45"></a>  var result = goog.labs.net.image.load(url);
<a name="line46"></a>
<a name="line47"></a>  goog.result.waitOnSuccess(result, function(value) {
<a name="line48"></a>
<a name="line49"></a>    assertEquals(goog.result.Result.State.SUCCESS, result.getState());
<a name="line50"></a>
<a name="line51"></a>    assertEquals(&#39;IMG&#39;, value.tagName);
<a name="line52"></a>    assertTrue(goog.string.endsWith(value.src, url));
<a name="line53"></a>    assertUndefined(result.getError());
<a name="line54"></a>
<a name="line55"></a>    assertEquals(&#39;Listeners should have been cleaned up.&#39;,
<a name="line56"></a>                 0, goog.events.getTotalListenerCount());
<a name="line57"></a>
<a name="line58"></a>    asyncTestCase.continueTesting();
<a name="line59"></a>  });
<a name="line60"></a>}
<a name="line61"></a>
<a name="line62"></a>function testInvalidImage() {
<a name="line63"></a>
<a name="line64"></a>  var url = &#39;testdata/invalid.gif&#39;; // This file does not exist.
<a name="line65"></a>
<a name="line66"></a>  asyncTestCase.waitForAsync(&#39;image load&#39;);
<a name="line67"></a>
<a name="line68"></a>  assertEquals(0, goog.events.getTotalListenerCount());
<a name="line69"></a>
<a name="line70"></a>  var result = goog.labs.net.image.load(url);
<a name="line71"></a>
<a name="line72"></a>  goog.result.wait(result, function(result) {
<a name="line73"></a>
<a name="line74"></a>    assertEquals(goog.result.Result.State.ERROR, result.getState());
<a name="line75"></a>    assertUndefined(result.getValue());
<a name="line76"></a>    assertUndefined(result.getError());
<a name="line77"></a>
<a name="line78"></a>    assertEquals(&#39;Listeners should have been cleaned up.&#39;,
<a name="line79"></a>                 0, goog.events.getTotalListenerCount());
<a name="line80"></a>
<a name="line81"></a>    asyncTestCase.continueTesting();
<a name="line82"></a>  });
<a name="line83"></a>}
<a name="line84"></a>
<a name="line85"></a>function testImageFactory() {
<a name="line86"></a>  var returnedImage = new Image();
<a name="line87"></a>  var factory = function() {
<a name="line88"></a>    return returnedImage;
<a name="line89"></a>  }
<a name="line90"></a>  var countedFactory = goog.testing.recordFunction(factory);
<a name="line91"></a>
<a name="line92"></a>  var url = &#39;testdata/cleardot.gif&#39;;
<a name="line93"></a>
<a name="line94"></a>  asyncTestCase.waitForAsync(&#39;image load&#39;);
<a name="line95"></a>  assertEquals(0, goog.events.getTotalListenerCount());
<a name="line96"></a>  var result = goog.labs.net.image.load(url, countedFactory);
<a name="line97"></a>
<a name="line98"></a>  goog.result.waitOnSuccess(result, function(value) {
<a name="line99"></a>    assertEquals(goog.result.Result.State.SUCCESS, result.getState());
<a name="line100"></a>    assertEquals(returnedImage, value);
<a name="line101"></a>    assertEquals(1, countedFactory.getCallCount());
<a name="line102"></a>    assertUndefined(result.getError());
<a name="line103"></a>
<a name="line104"></a>    assertEquals(&#39;Listeners should have been cleaned up.&#39;,
<a name="line105"></a>                 0, goog.events.getTotalListenerCount());
<a name="line106"></a>    asyncTestCase.continueTesting();
<a name="line107"></a>  });
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>function testExistingImage() {
<a name="line111"></a>  var image = new Image();
<a name="line112"></a>
<a name="line113"></a>  var url = &#39;testdata/cleardot.gif&#39;;
<a name="line114"></a>
<a name="line115"></a>  asyncTestCase.waitForAsync(&#39;image load&#39;);
<a name="line116"></a>  assertEquals(0, goog.events.getTotalListenerCount());
<a name="line117"></a>  var result = goog.labs.net.image.load(url, image);
<a name="line118"></a>
<a name="line119"></a>  goog.result.waitOnSuccess(result, function(value) {
<a name="line120"></a>    assertEquals(goog.result.Result.State.SUCCESS, result.getState());
<a name="line121"></a>    assertEquals(image, value);
<a name="line122"></a>    assertUndefined(result.getError());
<a name="line123"></a>
<a name="line124"></a>    assertEquals(&#39;Listeners should have been cleaned up.&#39;,
<a name="line125"></a>                 0, goog.events.getTotalListenerCount());
<a name="line126"></a>    asyncTestCase.continueTesting();
<a name="line127"></a>  });
<a name="line128"></a>}
</pre>


</body>
</html>
